﻿
Public MustInherit Class perceptron
    Inherits visitCounter

    Public Shared Shadows _pathidentity As String = "/perceptron"

    Private Class query
        Public Const key As String = webctrl.ping.query.key
        Public Const operation As String = webctrl.ping.query.operation
    End Class

    Private Class operations
        Public Const increase As String = webctrl.ping.operations.increase
        Public Const decrease As String = webctrl.ping.operations.decrease
    End Class

    Public Overrides Function pathidentity() As String
        Return _pathidentity
    End Function

    Public Shared Function generatePerceptronLink(ByVal key As String, ByVal increase As Boolean) As String
        If increase Then
            Return urlUtils.appendQuery(strmid(_pathidentity, strlen(network.pathSeperator)) _
                                        , query.key, key, query.operation, operations.increase)
        Else
            Return urlUtils.appendQuery(strmid(_pathidentity, strlen(network.pathSeperator)) _
                                        , query.key, key, query.operation, operations.decrease)
        End If
    End Function

    Protected Overrides Function processHomepage(ByVal context As httpContext) As Boolean
        'no homepage in perceptron.
        context.setstatuscode(Net.HttpStatusCode.NoContent, "NOCONTENT")
        Return True
    End Function

    Protected Overrides Function processOutput(ByVal context As httpContext, ByVal count As Long) As Boolean
        'no output in perceptron.
        context.setstatuscode(Net.HttpStatusCode.NoContent, "NOCONTENT")
        Return True
    End Function

    Public Overrides Function process(ByVal context As httpContext) As Boolean
        Dim key As String = Nothing
        key = context.url().query(query.key)
        Dim operation As String = Nothing
        operation = context.url().query(query.operation)
        If Not isemptystring(key) AndAlso Not isemptystring(operation) Then
            If strsame(operation, operations.increase, False) Then
                Return processIncrease(context, key)
            Else
                Return processDecrease(context, key)
            End If
        Else
            raiseError("do not get valid key or operation in url, ignore request.", errorHandle.errorType.user)
            Return False
        End If
    End Function
End Class
